import { observable, action, autorun, toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
import { ListStore } from './listStore';
import { WeaTools } from 'ecCom';
import * as Apis from '../apis/contact';
import { CrmContactStore } from './crmContactStore';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class ContactStore extends ListStore {
    @observable title = '';
    @observable rightMenu = [];
    @observable remindCount = 0;
    @observable detailTabKey = 'info';
    @observable remindTableStore = new TableStore();
    @observable crmContactStore = new CrmContactStore();
    @observable remindInfo = {};
    @observable workPlanvisible = false;
    @observable contactId = '';
    @observable workPlanActiveKey = '1';
    @observable isInitFinish = false;

    @action
    initDatas = (params = {}) => {
      if (params.tabtype && params.tabtype === 'remind') {
        this.changeTab('remind');
      } else {
        Apis.getContactCondition({ ...params, tabtype: this.detailTabKey }).then((data) => {
          this.condition = data.datas;
          // 根据高级搜索条件初始化form
          if (this.form.isFormInit) {
            this.form = new WeaForm();
          }
          this.form && !this.form.isFormInit && this.form.initFormFields(data.datas);
          this.isInitFinish = true;
          this.doSearch();
        });
      }
    }

    @action
    doSearch = (params = {}) => {
      if (params.tabtype && params.tabtype === 'remind') {
        this.changeTab('remind');
      } else {
        this.loading = true;
        // 获取表单的参数值
        const searchParamsAd = this.form.getFormParams();
        const newParams = { ...searchParamsAd, ...params, selectType: this.detailTabKey };
        Apis.getContactList(newParams).then((data) => {
          this.tableStore.getDatas(data.sessionkey, params.current || 1);
          this.dataKey = data.sessionkey;
          this.loading = false;
        });
      }
    }
    getRemindCount = (params = {}) => {
      Apis.getRemindCount(params).then((data) => {
        this.remindCount = data.datas;
      });
    }
    getRemindList = (params = {}) => {
      this.loading = true;
      Apis.getContactLogRemindList(params).then((data) => {
        this.remindTableStore.getDatas(data.sessionkey, params.current || 1);
        this.dataKey = data.sessionkey;
        this.loading = false;
        this.isInitFinish = true;
      });
    }

    @action
    changeTab = (key) => {
      this.detailTabKey = key;
      Apis.getContactCondition({ tabtype: this.detailTabKey }).then((data) => {
        this.condition = data.datas;
        // 根据高级搜索条件初始化form
        if (this.form.isFormInit) {
          this.form = new WeaForm();
        }
        this.form && !this.form.isFormInit && this.form.initFormFields(data.datas);
        this.isInitFinish = true;
        if (key == 'remind') {
          this.getRemindList();
        } else if (key == 'plan') {
          this.appendFormFields({ finished: { value: '0' } });
          this.doSearch({ selectType: key });
        } else {
          this.doSearch({ selectType: key });
        }
      });
    }

    finishWorkPlan = (params = {}) => {
      Apis.getFinishWorkPlan(params).then((data) => {
        if (data.status == 'success') {
          window._crmTable.reLoad();
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    onShowColumn = () => { // 显示定制列
      if (this.detailTabKey == 'remind') {
        this.remindTableStore.setColSetVisible(true);
        this.remindTableStore.tableColSet(true);
      } else {
        this.tableStore.setColSetVisible(true);
        this.tableStore.tableColSet(true);
      }
    }
    getRemindInfo = (id) => {
      Apis.getRemindInfo({ customerId: id }).then((data) => {
        if (data.status == 'success') {
          this.remindInfo = data.datas;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    saveRemindSetting = (params = {}) => {
      Apis.saveRemindSetting(params).then((data) => {
        if (data.status == 'success') {
          return true;
        }
        message.error(getLabel(83437, '出现错误！') + data.msgcode);
      });
    }

    clearStatus = () => {
      this.showSearchAd = false;
      this.detailTabKey = 'info';
      this.clearFormFields();
    }
    setWorkPlanVisible = (bool, id) => {
      this.workPlanvisible = bool;
      if (bool) {
        this.contactId = id;
      }
    }
    changeWorkPlanTab = (key) => {
      this.workPlanActiveKey = key;
    }

    doExportCurrent=() => {
      // WeaTools.callApi('/api/crm/contactLog/doContactLogExp', 'GET', {}).then(data=>{
      //     // alert(data)
      //     window.location="/weaver/weaver.file.ExcelOut";
      // });
      this.tableStore.exportCurrent();
    }

    doExport=() => {
      // WeaTools.callApi('/api/crm/contactLog/doContactLogExp', 'GET', {}).then(data=>{
      //     // alert(data)
      //     window.location="/weaver/weaver.file.ExcelOut";
      // });
      this.tableStore.exportAll();
    }
}

